// packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling/handling.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/process_supervision_api",
    fs_upload_url: "https://logisticsengineering.gree.com/process_supervision_api/photo_manage",
    page: 1,
    page_size: 10,
    activeNamesDetail: ['1'],
    activeNamesStep: ['1'],
    steps: [

    ],
    problem_source: "监督检查",
    active: 0,
    now_steps: {},
    opration: {
      "问题提交": ["待责任人整改报完工"],
      "待责任人整改报完工": ["整改完成，待复查人员确认"],
      "整改完成，待复查人员确认": ["完成整改，关闭问题", "待责任人整改报完工"],
    },
    Map: {
      "待责任人整改报完工": ["整改报完工"],
      "整改完成，待复查人员确认": ["完成整改", '未完成整改'],
    },
    btn_list: [],
    zgwg_show: false,
    yqzg_show: false,
    wg_show: false,
    action: "",
    files: [],
    reason: "",
    deadline: "",
    set_year: 0,
    set_month: 0,
    set_day: 0,
    zg_show: false,
    back_flag: false

  },
  // 上传照片
  getUploaderVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },

  // 延期时间选择
  setPickerYearValueClick(e) {
    let rect_deadline = this.data.task_data.rect_deadline.slice(0, 10)
    let pre_rect_deadline = this.data.task_data.rect_deadline.slice(0, 10)
    if (this.data.task_data.pre_rect_deadline != null) {
      pre_rect_deadline = this.data.task_data.pre_rect_deadline.slice(0, 10)
    }
    let show_date1 = pre_rect_deadline.slice(5, 10)
    let show_date2 = e.detail.slice(5, 10)
    if (e.detail < rect_deadline) {
      wx.showToast({
        title: '整改节点需大于当前整改节点！',
        icon: 'none'
      })
      this.setData({
        date_style: "",
        deadline: pre_rect_deadline,
        deadline1: show_date1,

      })
      this.handleDateF()
    } else {
      this.setData({
        date_style: "",
        deadline: e.detail,
        deadline1: show_date2,
      })
    }

  },
  // 延期日期显示处理
  handleDateF() {
    let childObj = this.selectComponent('.my-calendar')
    childObj.dateInitSet(this.data.set_year, this.data.set_month, this.data.set_day)
  },
  // 获取待办数据
  getHandlingData() {
    let url = this.data.baseUrl + "/get_current_5s_problem"

    let data = {
      page: this.data.page,
      page_size: this.data.page_size,

    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          list: res.data
        })
      } else {
        this.setData({
          list: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        list: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 展开详情
  onActiveDetailChange(event) {
    this.setData({
      activeNamesDetail: event.detail,
    });
  },
  // 展开步骤
  onActiveChangeStep(event) {
    this.setData({
      activeNamesStep: event.detail,
    });
  },
  // 处理签审日志数据
  handleStep() {
    let live_record1 = JSON.parse(JSON.stringify(this.data.task_data.live_record))
    for (let i in live_record1) {
      live_record1[i].time = this.GMTToStr(live_record1[i].time)
    }
    let step = []
    for (let i in live_record1) {
      let obj = {
        // text: live_record1[i].action,
        desc: live_record1[i].involved_name + "，" + live_record1[i].time + "，" + live_record1[i].action,
        detail: live_record1[i]
      }
      step.push(obj)
    }
    this.setData({
      steps: step,
      active: live_record1.length - 1,
    })


  },
  // 点击记录步骤详情弹窗显示
  showStep(e) {

    if (e.detail != 0) {
      let step_arr = JSON.parse(JSON.stringify(this.data.steps[e.detail].detail))
      let now_step = {}
      now_step.reason = step_arr.reason
      now_step.files = step_arr.files
      this.setData({
        step_detail_show: true,
        now_steps: now_step
      })
    }



  },
  // 点击记录步骤详情弹窗关闭
  onStepClose() {
    this.setData({
      now_steps: {},
      step_detail_show: false,

    })
  },
  // 处理步骤状态按钮显示
  handleStats() {
    let status = this.data.task_data.status
    let map_arr = this.data.Map
    let map_keys = Object.keys(map_arr)
    for (let i = 0; i < map_keys.length; i++) {
      if (map_keys[i] == status) {
        this.setData({
          btn_list: map_arr[map_keys[i]]
        })
      }

    }
  },
  // 处理按钮
  handleBtn(e) {
    console.log(e);
    let index = e.currentTarget.dataset.key
    let key_str = e.currentTarget.dataset.key_str
    let opration_arr = this.data.opration
    let current_status = this.data.task_data.status
    console.log("传过去的状态", opration_arr[current_status][index]);
    this.setData({
      action: opration_arr[current_status][index],
      zgwg_show: true,
    })
    if (key_str == "整改报完工") {
      this.setData({
        zg_show: true

      })
    }

  },
  // 整改弹窗确认
  onConfirm1() {
    if (this.data.reason == "") {
      wx.showToast({
        title: '说明不能为空',
        icon: 'none'
      })
    } else if (this.data.files.length == 0) {
      wx.showToast({
        title: '整改照片不能为空',
        icon: 'none'
      })
    }
    else {
      let url = this.data.baseUrl + "/luoyang_supervision_manage/update_process_problem"

      let data = {
        key: this.data.task_data._id,
        status: this.data.task_data.status,
        action: this.data.action,
        reason: this.data.reason,
        files: this.data.files,
      }
      console.log(data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageB/pages/package/task_manager/task_manager',
            })
          }, 2000)

        } else {

          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 撤销任务单
  callBackBtn() {
    wx.showModal({
      title: '确认撤销此单据？',
      content: '',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          let url = this.data.baseUrl + "/luoyang_supervision_manage/recall_process_problem"

          let data = {
            key: this.data.task_data.key,
            status: this.data.task_data.status,
            reason: this.data.reason,
            files: this.data.files,
          }
          console.log(data);
          this.post(url, data).then(res => {
            if (res.code == 20000) {
              wx.showToast({
                title: res.msg,
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/packageB/pages/package/task_manager/task_manager',
                })
              }, 2000)

            } else {

              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {

            console.log(err);
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
        }
      }
    })
  },
  // 整改弹窗关闭
  onHandleClose1() {
    this.setData({
      zgwg_show: false,
      reason: "",
      action: "",
      files: [],

    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getHandlingData()
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('toHandle', function (data) {
      console.log("传过来的数据", data)
      let technological_requirements_name = data.task.technological_requirements.split('：')[0]
      let technological_requirements_content = data.task.technological_requirements.split('：')[1]

      self.setData({
        task_data: data.task,
        current_involved_name: data.current_involved_name,
        business_content: data.business_content,
        current_process: data.current_process,
        technological_requirements_content: technological_requirements_content,
        technological_requirements_name: technological_requirements_name,
        responsible_person: data.responsible_person,
        creator_name: data.creator_name

      })
      self.handleStats()
    })
    eventChannel.on('showDetail', function (data) {
      console.log("传过来的数据", data)
      wx.setNavigationBarTitle({
        title: '工艺监督-已办/已发起任务'
      })
      let technological_requirements_name = data.task.technological_requirements.split('：')[0]
      let technological_requirements_content = data.task.technological_requirements.split('：')[1]
      if (data.task.live_record.length == 1) {
        self.setData({
          back_flag: true
        })
      }
      self.setData({
        task_data: data.task,
        current_involved_name: data.current_involved_name,
        business_content: data.business_content,
        current_process: data.current_process,
        technological_requirements_content: technological_requirements_content,
        technological_requirements_name: technological_requirements_name,
        creator_name: data.creator_name,
        responsible_person: data.responsible_person

      })
    })
    setTimeout(() => {
      self.handleStep()

    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})